﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using pcsse.App_Code.entity;
using System.Data.SqlClient;

namespace pcsse.WebService
{
    /// <summary>
    /// Summary description for PostService
    /// </summary>
    [WebService(Namespace = "http://sse.tongji.edu.cn/spb/a")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class PostService : System.Web.Services.WebService
    {

//Post
        [WebMethod]
        public DataTable GetUnexPostList(string sid)
        {
            if (!CheckSession(sid))
                return null;

            DataTable dt = db_base.getDBhandle().GetDataTableBySQL("SELECT ID, SUBJECT_ID, TITLE, STUDENT_ID, TIME FROM T_POST WHERE STATE=0");
            dt.TableName = "POST";

            return dt;
        }

        [WebMethod]
        public post_info_r GetUnexPost(string sid, string pid)
        {
            if (!CheckSession(sid))
                return null;

            SqlCommand cmd = new SqlCommand("SELECT * FROM T_POST WHERE ID=@ID");
            cmd.Parameters.Add("@ID", pid);
            DataTable dt = db_base.getDBhandle().GetDataTableByCmd(cmd);

            if (dt.Rows.Count == 1)
            {
                post_info_r post = new post_info_r();
                post.ID = pid;
                post.SUBJECT_ID = dt.Rows[0][1].ToString();
                post.TITLE = dt.Rows[0][2].ToString();
                post.TEXT = dt.Rows[0][3].ToString();
                post.STUDENT_ID = dt.Rows[0][4].ToString();
                post.TIME = dt.Rows[0][5].ToString();
                post.IP = dt.Rows[0][6].ToString();
                post.STATE = dt.Rows[0][7].ToString();

                return post;
            }

            return null;
        }

        [WebMethod]
        public bool UpdatePostState(string sid,string pid, string pstate)
        {
            if (!CheckSession(sid))
                return false;

            SqlCommand cmd = new SqlCommand("UPDATE T_POST SET STATE=@STATE WHERE ID=@ID");
            cmd.Parameters.Add("@STATE", pstate);
            cmd.Parameters.Add("@ID", pid);
            db_base.getDBhandle().ExecuteCmd(cmd);

            return false;
        }

//Reply
        [WebMethod]
        public DataTable GetUnexReplyList(string sid, string pid)
        {
            if (!CheckSession(sid))
                return null;

            SqlCommand cmd = new SqlCommand("SELECT ID, TEXT, STUDENT_ID, TIME FROM T_REPLY WHERE STATE=0 AND TITLE_ID=@TITLE_ID");
            cmd.Parameters.Add("@TITLE_ID", pid);
            DataTable dt = db_base.getDBhandle().GetDataTableByCmd(cmd);
            dt.TableName = "REPLY";

            return dt;
        }

        [WebMethod]
        public reply_info_r GetUnexReply(string sid, string pid)
        {
            if (!CheckSession(sid))
                return null;

            SqlCommand cmd = new SqlCommand("SELECT * FROM T_REPLY WHERE ID=@ID");
            cmd.Parameters.Add("@ID", pid);
            DataTable dt = db_base.getDBhandle().GetDataTableByCmd(cmd);

            if (dt.Rows.Count == 1)
            {
                reply_info_r reply = new reply_info_r();
                reply.ID = pid;
                reply.TITLE_ID = dt.Rows[0][1].ToString();
                reply.TEXT = dt.Rows[0][2].ToString();
                reply.STUDENT_ID = dt.Rows[0][3].ToString();
                reply.TIME = dt.Rows[0][4].ToString();
                reply.IP = dt.Rows[0][5].ToString();
                reply.STATE = dt.Rows[0][6].ToString();

                return reply;
            }

            return null;
        }

        [WebMethod]
        public bool UpdateReplyState(string sid, string pid, string pstate)
        {
            if (!CheckSession(sid))
                return false;

            SqlCommand cmd = new SqlCommand("UPDATE T_REPLY SET STATE=@STATE WHERE ID=@ID");
            cmd.Parameters.Add("@STATE", pstate);
            cmd.Parameters.Add("@ID", pid);
            db_base.getDBhandle().ExecuteCmd(cmd);

            return false;
        }

        private bool CheckSession(string sid)
        {
            DateTime dtime = new DateTime();
            SqlCommand cmd = new SqlCommand("SELECT LastTime FROM T_SESSION WHERE SID=@SID");
            cmd.Parameters.Add("@SID", sid);
            DataTable dt = db_base.getDBhandle().GetDataTableByCmd(cmd);
            if (dt.Rows.Count == 0)
                return false;
            //5分钟 过时
            else if ((DateTime.Now - Convert.ToDateTime(dt.Rows[0][0])).TotalMinutes > 4)
                return false;
            else
            {
                db_base.getDBhandle().ExecuteSql("UPDATE T_SESSION SET LastTIme='" + DateTime.Now + "' WHERE SID='" + sid + "'");
                return true;
            }

        }
    }
}
